package at.fhj.itm10.ticketmaster.dao.datasource;

import javax.annotation.Resource;
import javax.faces.context.ExternalContextFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.myfaces.context.ExternalContextFactoryImpl;

/**
 * Datasource locator.
 * 
 * @author Wolfgang Gaar
 */
public class DataSourceLocator {

	@Resource(name = "TicketmasterJDBC", type = DataSource.class)
	private DataSource dataSource;
	
	private static DataSourceLocator instance;

	/**
	 * Gets the datasource.
	 * @throws Exception 
	 * 
	 * @see http://tomee.apache.org/configuring-datasources.html
	 */
	private DataSourceLocator() throws Exception {
//		new ResourceXmlChecker();
		
		if (dataSource == null) {
			Context ctx = new InitialContext();
			dataSource = (DataSource) ctx
					.lookup("java:comp/env/TicketmasterJDBC");
		}
		
		// Should not happen
		if (dataSource == null) {
			throw new NullPointerException("dataSource is null");
		}
	}

	public static DataSource getDataSource() throws Exception {
		if (instance == null) {
			instance = new DataSourceLocator();
		}

		return instance.dataSource;
	}

}
